package com.itbaizhan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itbaizhan.domain.UserAccount;
import com.itbaizhan.mapper1.UserAccount1Mapper;
import com.itbaizhan.mapper2.UserAccount2Mapper;
import com.itbaizhan.service.UserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;


/**
 * @author 13487
 * @description 针对表【user_account】的数据库操作Service实现
 * @createDate 2024-04-14 20:45:29
 */
@Service
public class UserAccountServiceImpl implements UserAccountService {

    @Autowired
    private UserAccount1Mapper userAccount1Mapper;
    @Autowired
    private UserAccount2Mapper userAccount2Mapper;

    @Transactional
    @Override
    public void transfer(String accountNo1, String accountNo2, BigDecimal amount) {
        // 查询自己账号的信息
//        QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("account_no",accountNo1);
        UserAccount userAccount1 = userAccount1Mapper.selectById(accountNo1);
//        UserAccount userAccount1 = userAccountMapper1.selectOne(queryWrapper);

        // 查询对方的账号信息
        QueryWrapper<UserAccount> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("account_no", accountNo2);
        UserAccount userAccount2 = userAccount2Mapper.selectOne(queryWrapper1);

        if (userAccount1 != null && userAccount2 != null && (userAccount1.getAccountBalance().compareTo(amount) > 0)) {
            userAccount1.setAccountBalance(userAccount1.getAccountBalance().subtract(amount));
            userAccount1Mapper.updateById(userAccount1);
            int i = 10 / 0;

            userAccount2.setAccountBalance(userAccount2.getAccountBalance().add(amount));
            userAccount2Mapper.updateById(userAccount2);
        } else {
            throw new RuntimeException("账户不存在或者账户余额不足！");
        }

    }
}




